package io.github.luyanfei

import org.apache.commons.lang.builder.HashCodeBuilder

class GroupRole implements Serializable {

	private static final long serialVersionUID = 1

	Group group
	Role role

	boolean equals(other) {
		if (!(other instanceof GroupRole)) {
			return false
		}

		other.role?.id == role?.id &&
		other.group?.id == group?.id
	}

	int hashCode() {
		def builder = new HashCodeBuilder()
		if (group) builder.append(group.id)
		if (role) builder.append(role.id)
		builder.toHashCode()
	}

	static GroupRole get(long groupId, long roleId) {
		GroupRole.where {
			group == Group.load(groupId) &&
			role == Role.load(roleId)
		}.get()
	}

	static boolean exists(long groupId, long roleId) {
		GroupRole.where {
			group == Group.load(groupId) &&
			role == Role.load(roleId)
		}.count() > 0
	}

	static GroupRole create(Group group, Role role, boolean flush = false) {
		def instance = new GroupRole(group: group, role: role)
		instance.save(flush: flush, insert: true)
		instance
	}

	static boolean remove(Group rg, Role r, boolean flush = false) {
		if (rg == null || r == null) return false

		int rowCount = GroupRole.where {
			group == Group.load(rg.id) &&
			role == Role.load(r.id)
		}.deleteAll()

		if (flush) { GroupRole.withSession { it.flush() } }

		rowCount > 0
	}

	static void removeAll(Role r, boolean flush = false) {
		if (r == null) return

		GroupRole.where {
			role == Role.load(r.id)
		}.deleteAll()

		if (flush) { GroupRole.withSession { it.flush() } }
	}

	static void removeAll(Group rg, boolean flush = false) {
		if (rg == null) return

		GroupRole.where {
			group == Group.load(rg.id)
		}.deleteAll()

		if (flush) { GroupRole.withSession { it.flush() } }
	}

	static constraints = {
		role validator: { Role r, GroupRole rg ->
			if (rg.group == null) return
			boolean existing = false
			GroupRole.withNewSession {
				existing = GroupRole.exists(rg.group.id, r.id)
			}
			if (existing) {
				return 'roleGroup.exists'
			}
		}
	}

	static mapping = {
		id composite: ['group', 'role']
		version false
	}
}
